package com.central.modules.sys.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.central.common.exception.BizExceptionEnum;
import com.central.common.page.PageData;
import com.central.common.utils.JsonUtils;
import com.central.common.utils.Result;
import com.central.common.validator.ValidatorUtils;
import com.central.common.validator.group.AddGroup;
import com.central.common.validator.group.DefaultGroup;
import com.central.common.validator.group.UpdateGroup;
import com.central.modules.sys.entity.SysRoleEntity;
import com.central.modules.sys.model.AddSysRoleRequest;
import com.central.modules.sys.model.SetMenusToRoleRequest;
import com.central.modules.sys.model.SysRoleQueryBean;
import com.central.modules.sys.service.SysRoleMenuService;
import com.central.modules.sys.service.SysRoleService;
import com.central.modules.sys.service.SysRoleUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 角色管理
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys_role")
public class SysRoleController {

    private final SysRoleService sysRoleService;
    private final SysRoleMenuService roleMenuService;
    private final SysRoleUserService roleUserService;

    @GetMapping("/page_search")
    public Result<PageData<SysRoleEntity>> pageSearch(SysRoleQueryBean queryBean) {
        IPage<SysRoleEntity> page = sysRoleService.searchSysRoles(queryBean);
        return Result.succeed(PageData.copy(page, page.getRecords()));
    }

    @GetMapping("/all")
    public Result<List<SysRoleEntity>> allFind( ) {
        List<SysRoleEntity> sysRoleEntities = sysRoleService.findAll();
        return Result.succeed(sysRoleEntities);
    }

    @GetMapping("/detail")
    public Result<Map<String, Object>> detailById(@RequestParam Long id) {
        SysRoleEntity roleEntity = sysRoleService.getById(id);
        if (Objects.isNull(roleEntity)){
            return Result.failed(BizExceptionEnum.DATA_IS_NULL);
        }
        Map<String, Object> resMap = JsonUtils.toMap(roleEntity);
        resMap.put("menus", roleMenuService.listAllByRoleId(id));
        return Result.succeed(resMap);
    }


    @PostMapping("/add")
    public Result<Void> add(@RequestBody AddSysRoleRequest request) {
        //效验数据
        ValidatorUtils.validateEntity(request, AddGroup.class, DefaultGroup.class);
        sysRoleService.addSysRole(request);
        return Result.succeed(null);
    }

    @PutMapping("/update")
    public Result<Void> update(@RequestBody AddSysRoleRequest request) {
        //效验数据
        ValidatorUtils.validateEntity(request, UpdateGroup.class, DefaultGroup.class);
        sysRoleService.updateSysRole(request.getId(), request);
        return Result.succeed(null);
    }

    @DeleteMapping("/del")
    public Result<Void> del(@RequestParam Long id) {
        roleMenuService.cleanByRoleId(id);
        roleUserService.cleanByRoleId(id);
        sysRoleService.deleteSysRole(id);
        return Result.succeed(null);
    }

    //查询已经分配的权限
    @PostMapping("/set_menu")
    public Result<Void> setMenuIds(@RequestBody SetMenusToRoleRequest request) {
        ValidatorUtils.validateEntity(request, DefaultGroup.class);
        roleMenuService.add(request.getRoleId(), request.getMenuIds());

        return Result.succeed(null);
    }

    @PostMapping("/remove_user")
    public Result<Void> removeUser(@RequestBody Map<String,Object> reqMap) {
      Long roleId=  MapUtil.getLong(reqMap, "roleId");
      Long userId=  MapUtil.getLong(reqMap, "userId");
      roleUserService.removeByRoleIdAndUserId(roleId,userId);
        return Result.succeed(null);
    }
}
